﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using TGBTech.Core.Models.DanhMuc;
using TGB.Core.Infrastructure;

namespace TGBTech.Web.Controllers
{
    public class ChiTietHangHoaController : Controller
    {
        private TGBTechDb db = new TGBTechDb();

        // GET: /ChiTietHangHoa/
        public ActionResult Index()
        {
            var chitiethanghoa = db.ChiTietHangHoa.Include(c => c.HangHoa).Where(p=>p.State == true);
            
            return View(chitiethanghoa.ToList());
        }

        // GET: /ChiTietHangHoa/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ChiTietHangHoa chitiethanghoa = db.ChiTietHangHoa.Find(id);
            if (chitiethanghoa == null)
            {
                return HttpNotFound();
            }
            return View(chitiethanghoa);
        }

        // GET: /ChiTietHangHoa/Create
        public ActionResult Create()
        {
            ViewBag.HangHoaId = new SelectList(db.HangHoa, "Id", "Id");
            return PartialView();
        }

        // POST: /ChiTietHangHoa/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include="Id,HangHoaId,OtherName,SoLo,QuyCach,DVT,VAT,NgayNhap,NgayHetHan,DonGiaNhap,SoLuongTon,State")] ChiTietHangHoa chitiethanghoa)
        {
            if (ModelState.IsValid)
            {
                chitiethanghoa.State = true;
                db.ChiTietHangHoa.Add(chitiethanghoa);
                db.SaveChanges();

                return Json(new { Id = chitiethanghoa.HangHoaId,
                                  Name = db.HangHoa.Where(p=>p.Id == chitiethanghoa.HangHoaId).ToList()[0].Name,
                                  OtherName = chitiethanghoa.OtherName,
                                  SoLo = chitiethanghoa.SoLo,
                                  QuyCach = chitiethanghoa.QuyCach,
                                  DVT = chitiethanghoa.DVT,
                                  VAT = chitiethanghoa.VAT,
                                  NgayNhap = chitiethanghoa.NgayNhap,
                                  NgayHetHan = chitiethanghoa.NgayHetHan,
                                  DonGiaNhap = chitiethanghoa.DonGiaNhap,
                                  SoLuongTon = chitiethanghoa.SoLuongTon
                });
            }

            ViewBag.HangHoaId = new SelectList(db.HangHoa, "Id", "Name", chitiethanghoa.HangHoaId);
            return PartialView(chitiethanghoa);
        }

        // GET: /ChiTietHangHoa/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ChiTietHangHoa chitiethanghoa = db.ChiTietHangHoa.Find(id);
            if (chitiethanghoa == null)
            {
                return HttpNotFound();
            }
            ViewBag.HangHoaId = new SelectList(db.HangHoa, "Id", "Name", chitiethanghoa.HangHoaId);
            return PartialView(chitiethanghoa);
        }

        // POST: /ChiTietHangHoa/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include="Id,HangHoaId,OtherName,SoLo,QuyCach,DVT,VAT,NgayNhap,NgayHetHan,DonGiaNhap,SoLuongTon,State")] ChiTietHangHoa chitiethanghoa)
        {
            if (ModelState.IsValid)
            {
                db.Entry(chitiethanghoa).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.HangHoaId = new SelectList(db.HangHoa, "Id", "Name", chitiethanghoa.HangHoaId);
            return PartialView(chitiethanghoa);
        }

        // GET: /ChiTietHangHoa/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ChiTietHangHoa chitiethanghoa = db.ChiTietHangHoa.Find(id);
            if (chitiethanghoa == null)
            {
                return HttpNotFound();
            }
            return PartialView(chitiethanghoa);
        }

        // POST: /ChiTietHangHoa/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            ChiTietHangHoa chitiethanghoa = db.ChiTietHangHoa.Find(id);
            chitiethanghoa.State = false;
            db.Entry(chitiethanghoa).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
